﻿using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using LEDControllerTools.Commands;
using LEDControllerTools.Controller;
using LEDControllerTools.Scripting;
using Testpad.Properties;

namespace Testpad
{
    class Program
    {

        private static void Main(string[] args)
        {

            var commandFactory = Settings.Default.Controller as ICommandFactory;
            var controllable = Controller.GetInstance(commandFactory);
            controllable.PinToggled += controllable_PinToggled;
            RunLines(@".\aScript.txt", controllable);
            //RunScriptAsCode(controllable);
            //TestWithAsmHelperAndUnloadingAssembly();
            //SerializeDummyFactory(@"c:\Backup\test.xml");

        }

        private static void controllable_PinToggled(object sender, ToggledEventArgs e)
        {
            Console.WriteLine("Pin " + sender + ": " + e.IsOn);
        }


        static void RunLines(string scriptPath, IControllable controllable)
        {
            var engine = new ScriptEngine(scriptPath);
            engine.Run(controllable);
            do
            {

            } while (engine.IsBusy);
        }

        public static void RunScriptAsCode(IControllable controllable)
        {
            var script = new TestScript();
            script.Run(controllable);
            
        }


        //static void TestWithAsmHelperAndUnloadingAssembly()
        //{

        //    //            var Sum = new AsmHelper(CSScript.LoadMethod(
        //    //           @"public static int Sum(int a, int b)
        //    //              {
        //    //                  System.Diagnostics.Debugger.Break();
        //    //                  return a + b;
        //    //              }", null, true))
        //    //             .GetStaticMethod();

        //    //            int result = (int)Sum(1, 2);

        //    //we cannot use HelloScript.cs as class Script must 
        //    //be serializable or derived from MarshalByRefObject
        //    var code = ReadFile(@".\Script.cs");

        //    using (var helper = new AsmHelper(CSScript.CompileCode(code, null, true,new[] {@".\PIC.dll"}), null, true))
        //    {
        //        var commandFactory = Settings.Default.Controller.AlignToInterface<ICommandFactory>();
        //        var controllable = Controller.GetInstance(commandFactory);

        //        helper.Invoke("Run", controllable);
        //        IScript script = helper.CreateAndAlignToInterface<IScript>("Testpad.Script");

        //        script.Run(controllable);
        //    }
        //}


        static void SerializeDummyFactory(string path)
        {
            var cont = new Rs232CommandFactory
                {
                BaudRate = 1299,
                DataBits = 8,
                Parity = Parity.None,
                PortName = "COM20",
                StopBits = StopBits.One
            };
            var serializer = new XmlSerializer(typeof(Rs232CommandFactory));
            var tw = new StreamWriter(path);
            serializer.Serialize(tw, cont);
        }







    }
}
